/*
 * ServletFileUploadGateway.java
 *
 * Created on 29 de junio de 2007, 02:43 PM
 */

package servletfileuploadgateway;

import com.src.computec.radar.reclamos.base.constantes.ConstantesGenerales;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author aocampo
 * @version
 */
public class ServletFileUploadGateway extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected synchronized void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        boolean fileValid = false;
        try {
            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            diskFileItemFactory.setSizeThreshold(10000000);
            diskFileItemFactory.setRepository(new File(ConstantesGenerales.getInstance().FOLDER_TEMPORAL_FILE_UPLOAD));
            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
            servletFileUpload.setSizeMax(10000000);
            List listItems = servletFileUpload.parseRequest(request);
            Iterator iterator = listItems.iterator();
            while (iterator.hasNext()) {
                FileItem fileItem = (FileItem) iterator.next();
                if (!fileItem.isFormField()) { 
                    if (fileItem.getSize() <= 5000000) {
                        String originalName = fileItem.getName().replace("\\", "&");
                        String[] tokens = originalName.split("&");
                        String fileName = tokens[tokens.length - 1];
                        File file = new File(ConstantesGenerales.getInstance().FOLDER_FILE_UPLOAD + "directorio.emensajeria");
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        file = new File(ConstantesGenerales.getInstance().FOLDER_FILE_UPLOAD + "directorio.emensajeria" + File.separator + fileName);
                        fileItem.write(file);
                        fileValid = true;
                        response.sendRedirect("index.jsp?result=1&nameUploadedFile=directorio.emensajeria" + File.separator + fileName);
                    }
                    else {
                        fileValid = true;
                        response.sendRedirect("index.jsp?result=2");
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if (!fileValid) {
            System.out.println("ServletFileUploadGateway:processRequest:el archivo es invalido");
            response.sendRedirect("index.jsp?result=3");
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
